#include <sdk.h>
#include "editbreakpointdlg.h"

BEGIN_EVENT_TABLE( EditBreakpointDlg, wxScrollingDialog )
  EVT_UPDATE_UI( -1, EditBreakpointDlg::OnUpdateUI )
END_EVENT_TABLE()

EditBreakpointDlg::EditBreakpointDlg( const DebuggerBreakpoint &breakpoint, wxWindow* parent ) : m_breakpoint( breakpoint ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/breakpoints.xrc" );
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "dlgEditBreakpoint" ), _T( "wxScrollingDialog" ) ) ) {
    return;
  }
  XRCCTRL( *this, "chkEnabled", wxCheckBox )->SetValue( m_breakpoint.enabled );
  XRCCTRL( *this, "chkIgnore", wxCheckBox )->SetValue( m_breakpoint.useIgnoreCount );
  XRCCTRL( *this, "spnIgnoreCount", wxSpinCtrl )->SetValue( m_breakpoint.ignoreCount );
  XRCCTRL( *this, "chkExpr", wxCheckBox )->SetValue( m_breakpoint.useCondition );
  XRCCTRL( *this, "txtExpr", wxTextCtrl )->SetValue( m_breakpoint.condition );
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  SetMaxSize( wxSize( -1, GetMinHeight() ) );
}

EditBreakpointDlg::~EditBreakpointDlg() {
}

void EditBreakpointDlg::EndModal( int retCode ) {
  if( retCode == wxID_OK ) {
    m_breakpoint.enabled = XRCCTRL( *this, "chkEnabled", wxCheckBox )->GetValue();
    m_breakpoint.useIgnoreCount = XRCCTRL( *this, "chkIgnore", wxCheckBox )->IsChecked();
    m_breakpoint.ignoreCount = XRCCTRL( *this, "spnIgnoreCount", wxSpinCtrl )->GetValue();
    m_breakpoint.useCondition = XRCCTRL( *this, "chkExpr", wxCheckBox )->IsChecked();
    m_breakpoint.condition = CleanStringValue( XRCCTRL( *this, "txtExpr", wxTextCtrl )->GetValue() );
  }
  wxScrollingDialog::EndModal( retCode );
}

void EditBreakpointDlg::OnUpdateUI( M_Code_unused wxUpdateUIEvent& event ) {
  bool en = XRCCTRL( *this, "chkEnabled", wxCheckBox )->IsChecked();
  XRCCTRL( *this, "chkIgnore", wxCheckBox )->Enable( en && !XRCCTRL( *this, "chkExpr", wxCheckBox )->IsChecked() );
  XRCCTRL( *this, "spnIgnoreCount", wxSpinCtrl )->Enable( en && XRCCTRL( *this, "chkIgnore", wxCheckBox )->IsChecked() );
  XRCCTRL( *this, "chkExpr", wxCheckBox )->Enable( en && !XRCCTRL( *this, "chkIgnore", wxCheckBox )->IsChecked() );
  XRCCTRL( *this, "txtExpr", wxTextCtrl )->Enable( en && XRCCTRL( *this, "chkExpr", wxCheckBox )->IsChecked() );
}
